package com.example.smart.dialog

import android.content.Context
import android.view.Gravity
import android.view.View
import com.alibaba.fastjson.JSON
import com.example.lib.extend.getDeviceWidth
import com.example.lib.extend.setOnDoubleClickListener
import com.example.lib.utils.sp.SpKey
import com.example.lib.utils.sp.SpUtils
import com.example.lib.widget.wheelview.annotation.ScrollState
import com.example.lib.widget.wheelview.contract.OnWheelChangedListener
import com.example.lib.widget.wheelview.widget.WheelView
import com.example.smart.base.dialog.base.BaseDialog
import com.example.smart.data.AddressData
import com.example.smart.databinding.DialogAddressBinding


class AddressDialog(context: Context) : BaseDialog<DialogAddressBinding>(dialogContext = context,
    width = (getDeviceWidth(context) * 0.95).toInt(),
    gravity = Gravity.BOTTOM) {
    var onResult: ((String) -> Unit)? = null
    var onResult2: ((String, String) -> Unit)? = null

    override fun setLayoutRes() = com.example.smart.R.layout.dialog_address

    override fun afterViewCreate(view: View?) {
        val text =
            "{\"code\":200,\"msg\":\"ok\",\"time\":1745569147,\"data\":[{\"code\":\"150000\",\"name\":\"内蒙古自治区\",\"childs\":[{\"code\":\"150100\",\"name\":\"呼和浩特市\",\"childs\":[{\"code\":\"150102\",\"name\":\"新城区\"},{\"code\":\"150103\",\"name\":\"回民区\"},{\"code\":\"150104\",\"name\":\"玉泉区\"},{\"code\":\"150105\",\"name\":\"赛罕区\"},{\"code\":\"150121\",\"name\":\"土默特左旗\"},{\"code\":\"150122\",\"name\":\"托克托县\"},{\"code\":\"150123\",\"name\":\"和林格尔县\"},{\"code\":\"150124\",\"name\":\"清水河县\"},{\"code\":\"150125\",\"name\":\"武川县\"},{\"code\":\"150172\",\"name\":\"呼和浩特经济技术开发区\"}]},{\"code\":\"150200\",\"name\":\"包头市\",\"childs\":[{\"code\":\"150202\",\"name\":\"东河区\"},{\"code\":\"150203\",\"name\":\"昆都仑区\"},{\"code\":\"150204\",\"name\":\"青山区\"},{\"code\":\"150205\",\"name\":\"石拐区\"},{\"code\":\"150206\",\"name\":\"白云鄂博矿区\"},{\"code\":\"150207\",\"name\":\"九原区\"},{\"code\":\"150221\",\"name\":\"土默特右旗\"},{\"code\":\"150222\",\"name\":\"固阳县\"},{\"code\":\"150223\",\"name\":\"达尔罕茂明安联合旗\"},{\"code\":\"150271\",\"name\":\"包头稀土高新技术产业开发区\"}]},{\"code\":\"150300\",\"name\":\"乌海市\",\"childs\":[{\"code\":\"150302\",\"name\":\"海勃湾区\"},{\"code\":\"150303\",\"name\":\"海南区\"},{\"code\":\"150304\",\"name\":\"乌达区\"}]},{\"code\":\"150400\",\"name\":\"赤峰市\",\"childs\":[{\"code\":\"150402\",\"name\":\"红山区\"},{\"code\":\"150403\",\"name\":\"元宝山区\"},{\"code\":\"150404\",\"name\":\"松山区\"},{\"code\":\"150421\",\"name\":\"阿鲁科尔沁旗\"},{\"code\":\"150422\",\"name\":\"巴林左旗\"},{\"code\":\"150423\",\"name\":\"巴林右旗\"},{\"code\":\"150424\",\"name\":\"林西县\"},{\"code\":\"150425\",\"name\":\"克什克腾旗\"},{\"code\":\"150426\",\"name\":\"翁牛特旗\"},{\"code\":\"150428\",\"name\":\"喀喇沁旗\"},{\"code\":\"150429\",\"name\":\"宁城县\"},{\"code\":\"150430\",\"name\":\"敖汉旗\"}]},{\"code\":\"150500\",\"name\":\"通辽市\",\"childs\":[{\"code\":\"150502\",\"name\":\"科尔沁区\"},{\"code\":\"150521\",\"name\":\"科尔沁左翼中旗\"},{\"code\":\"150522\",\"name\":\"科尔沁左翼后旗\"},{\"code\":\"150523\",\"name\":\"开鲁县\"},{\"code\":\"150524\",\"name\":\"库伦旗\"},{\"code\":\"150525\",\"name\":\"奈曼旗\"},{\"code\":\"150526\",\"name\":\"扎鲁特旗\"},{\"code\":\"150581\",\"name\":\"霍林郭勒市\"},{\"code\":\"150571\",\"name\":\"通辽经济技术开发区\"}]},{\"code\":\"150600\",\"name\":\"鄂尔多斯市\",\"childs\":[{\"code\":\"150602\",\"name\":\"东胜区\"},{\"code\":\"150603\",\"name\":\"康巴什区\"},{\"code\":\"150621\",\"name\":\"达拉特旗\"},{\"code\":\"150622\",\"name\":\"准格尔旗\"},{\"code\":\"150623\",\"name\":\"鄂托克前旗\"},{\"code\":\"150624\",\"name\":\"鄂托克旗\"},{\"code\":\"150625\",\"name\":\"杭锦旗\"},{\"code\":\"150626\",\"name\":\"乌审旗\"},{\"code\":\"150627\",\"name\":\"伊金霍洛旗\"}]},{\"code\":\"150700\",\"name\":\"呼伦贝尔市\",\"childs\":[{\"code\":\"150702\",\"name\":\"海拉尔区\"},{\"code\":\"150703\",\"name\":\"扎赉诺尔区\"},{\"code\":\"150721\",\"name\":\"阿荣旗\"},{\"code\":\"150722\",\"name\":\"莫力达瓦达斡尔族自治旗\"},{\"code\":\"150723\",\"name\":\"鄂伦春自治旗\"},{\"code\":\"150724\",\"name\":\"鄂温克族自治旗\"},{\"code\":\"150725\",\"name\":\"陈巴尔虎旗\"},{\"code\":\"150726\",\"name\":\"新巴尔虎左旗\"},{\"code\":\"150727\",\"name\":\"新巴尔虎右旗\"},{\"code\":\"150781\",\"name\":\"满洲里市\"},{\"code\":\"150782\",\"name\":\"牙克石市\"},{\"code\":\"150783\",\"name\":\"扎兰屯市\"},{\"code\":\"150784\",\"name\":\"额尔古纳市\"},{\"code\":\"150785\",\"name\":\"根河市\"}]},{\"code\":\"150800\",\"name\":\"巴彦淖尔市\",\"childs\":[{\"code\":\"150802\",\"name\":\"临河区\"},{\"code\":\"150821\",\"name\":\"五原县\"},{\"code\":\"150822\",\"name\":\"磴口县\"},{\"code\":\"150823\",\"name\":\"乌拉特前旗\"},{\"code\":\"150824\",\"name\":\"乌拉特中旗\"},{\"code\":\"150825\",\"name\":\"乌拉特后旗\"},{\"code\":\"150826\",\"name\":\"杭锦后旗\"}]},{\"code\":\"150900\",\"name\":\"乌兰察布市\",\"childs\":[{\"code\":\"150902\",\"name\":\"集宁区\"},{\"code\":\"150921\",\"name\":\"卓资县\"},{\"code\":\"150922\",\"name\":\"化德县\"},{\"code\":\"150923\",\"name\":\"商都县\"},{\"code\":\"150924\",\"name\":\"兴和县\"},{\"code\":\"150925\",\"name\":\"凉城县\"},{\"code\":\"150926\",\"name\":\"察哈尔右翼前旗\"},{\"code\":\"150927\",\"name\":\"察哈尔右翼中旗\"},{\"code\":\"150928\",\"name\":\"察哈尔右翼后旗\"},{\"code\":\"150929\",\"name\":\"四子王旗\"},{\"code\":\"150981\",\"name\":\"丰镇市\"}]},{\"code\":\"152200\",\"name\":\"兴安盟\",\"childs\":[{\"code\":\"152201\",\"name\":\"乌兰浩特市\"},{\"code\":\"152202\",\"name\":\"阿尔山市\"},{\"code\":\"152221\",\"name\":\"科尔沁右翼前旗\"},{\"code\":\"152222\",\"name\":\"科尔沁右翼中旗\"},{\"code\":\"152223\",\"name\":\"扎赉特旗\"},{\"code\":\"152224\",\"name\":\"突泉县\"}]},{\"code\":\"152500\",\"name\":\"锡林郭勒盟\",\"childs\":[{\"code\":\"152501\",\"name\":\"二连浩特市\"},{\"code\":\"152502\",\"name\":\"锡林浩特市\"},{\"code\":\"152522\",\"name\":\"阿巴嘎旗\"},{\"code\":\"152523\",\"name\":\"苏尼特左旗\"},{\"code\":\"152524\",\"name\":\"苏尼特右旗\"},{\"code\":\"152525\",\"name\":\"东乌珠穆沁旗\"},{\"code\":\"152526\",\"name\":\"西乌珠穆沁旗\"},{\"code\":\"152527\",\"name\":\"太仆寺旗\"},{\"code\":\"152528\",\"name\":\"镶黄旗\"},{\"code\":\"152529\",\"name\":\"正镶白旗\"},{\"code\":\"152530\",\"name\":\"正蓝旗\"},{\"code\":\"152531\",\"name\":\"多伦县\"},{\"code\":\"152571\",\"name\":\"乌拉盖管委会\"}]},{\"code\":\"152900\",\"name\":\"阿拉善盟\",\"childs\":[{\"code\":\"152921\",\"name\":\"阿拉善左旗\"},{\"code\":\"152922\",\"name\":\"阿拉善右旗\"},{\"code\":\"152923\",\"name\":\"额济纳旗\"},{\"code\":\"152971\",\"name\":\"内蒙古阿拉善高新技术产业开发区\"}]}]}]}"
        val json: AddressData = JSON.parseObject(text, AddressData::class.java)
        binding.DialogWheel1.setOnWheelChangedListener(object : OnWheelChangedListener() {

            override fun onWheelScrollStateChanged(view: WheelView?, state: Int) {
                if (state == ScrollState.SCROLLING || state == ScrollState.DRAGGING) {
                    binding.DialogWheel2.isEnabled = false
                    binding.DialogWheel3.isEnabled = false
                } else {
                    binding.DialogWheel2.isEnabled = true
                    binding.DialogWheel3.isEnabled = true
                }
            }

            override fun onWheelSelected(view: WheelView?, position: Int) {
                binding.DialogWheel2.data = json.data[position].childs
            }
        })

        binding.DialogWheel1.setFormatter { item ->
            return@setFormatter (item as AddressData.DataDTO).name
        }

        binding.DialogWheel2.setOnWheelChangedListener(object : OnWheelChangedListener() {

            override fun onWheelScrollStateChanged(view: WheelView?, state: Int) {
                if (state == ScrollState.SCROLLING || state == ScrollState.DRAGGING) {
                    binding.DialogWheel1.isEnabled = false
                    binding.DialogWheel3.isEnabled = false
                } else {
                    binding.DialogWheel1.isEnabled = true
                    binding.DialogWheel3.isEnabled = true
                }
            }


            override fun onWheelSelected(view: WheelView?, position: Int) {
                binding.DialogWheel3.data = json.data[binding.DialogWheel1.currentPosition].childs[position].childs
            }
        })

        binding.DialogWheel2.setFormatter { item ->
            return@setFormatter (item as AddressData.DataDTO.ChildsDTO).name
        }

        binding.DialogWheel3.setOnWheelChangedListener(object : OnWheelChangedListener() {

            override fun onWheelScrollStateChanged(view: WheelView?, state: Int) {
                if (state == ScrollState.SCROLLING || state == ScrollState.DRAGGING) {
                    binding.DialogWheel1.isEnabled = false
                    binding.DialogWheel2.isEnabled = false
                } else {
                    binding.DialogWheel1.isEnabled = true
                    binding.DialogWheel2.isEnabled = true
                }
            }

            override fun onWheelSelected(view: WheelView?, position: Int) {


            }
        })

        binding.DialogWheel3.setFormatter { item ->
            return@setFormatter (item as AddressData.DataDTO.ChildsDTO).name
        }

        binding.DialogWheel1.data = json.data
        binding.DialogWheel2.data = json.data[0].childs
        binding.DialogWheel3.data = json.data[0].childs[0].childs

        binding.DialogClose.setOnDoubleClickListener {
            dismiss()
        }

        binding.DialogConfirm.setOnDoubleClickListener {
            dismiss()
            if (onResult2 == null) {
                SpUtils.putString(SpKey.ADDRESS,
                    json.data[binding.DialogWheel1.currentPosition].childs[binding.DialogWheel2.currentPosition].childs[binding.DialogWheel3.currentPosition].name)
                onResult?.invoke(json.data[binding.DialogWheel1.currentPosition].childs[binding.DialogWheel2.currentPosition].childs[binding.DialogWheel3.currentPosition].name)
            } else {
                onResult2?.invoke("${json.data[binding.DialogWheel1.currentPosition].name},${json.data[binding.DialogWheel1.currentPosition].childs[binding.DialogWheel2.currentPosition].name}",
                    json.data[binding.DialogWheel1.currentPosition].childs[binding.DialogWheel2.currentPosition].childs[binding.DialogWheel3.currentPosition].name)
            }
        }
    }


}